﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Common;

namespace Hospital
{
    public partial class PatientUserControl : UserControl
    {
        private bool hasId = false;

        public PatientUserControl()
        {
            InitializeComponent();
        }

        public PatientUserControl(int id, string name)
        {
            InitializeComponent();
            FillForm(id, name);
        }
        private void FillForm(int id, string name)
        {
            this.hasId = true;
            Patient p = null;
            if (name == string.Empty)
            {
                p = PatientDao.getById(id);
            }
            else
            {
                p = PatientDao.getByIdAndName(id, name);
            }
            if (p != null)
            {
                this.tbId.Text = p.Id.ToString();
                this.tbName.Text = p.Name;
                this.cbGender.Text = p.Gender;
                this.dtBirthday.Value = p.Birthday;
                this.tbTelephone.Text = p.Telephone;
                this.tbAddress.Text = p.Address;
                this.tbBirthWeight.Text = p.BirthWeight.ToString();
                this.cbBirthSituation.Text = p.BirthSituation;
                this.cbChildBirth.Text = p.Childbirth;

                this.btnAddPatient.Visible = false;
                this.btnUpdatePatient.Visible = true;
                this.tbId.ReadOnly = true;

                //检查复诊情况和化验情况
                DateTime lastDiagnoseDate = DiagnoseDao.getLastDiagnoseDate(id);
                DateTime? lastExaminationDate = DateTime.Now;
                List<Diagnose> diagnoseList = DiagnoseDao.getAllPatientDiagnoses(id);
                if (diagnoseList.Count > 0)
                {
                    lastExaminationDate = null;
                    foreach (Diagnose diagnose in diagnoseList)
                    {
                        if (PatientExaminationDao.getAllPatientExaminationsCount(diagnose.Id, "Diagnose") > 0)
                        {
                            lastExaminationDate = diagnose.CreateDate;
                            break;
                        }
                    }
                }

                StringBuilder strErrorMsg = new StringBuilder();
                if (lastDiagnoseDate.AddMonths(2) < DateTime.Now)
                {
                    strErrorMsg.Append("病人已超过2个月没来复诊\n");
                }
                if (lastExaminationDate == null || ((DateTime)lastExaminationDate).AddMonths(3) < DateTime.Now)
                {
                    strErrorMsg.Append("病人已超过3个月没做化验\n");
                }
                if (strErrorMsg.Length > 0)
                {
                    MessageBox.Show(strErrorMsg.ToString(), "出错提示");

                    return;
                }

            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            StringBuilder strErrorMsg = new StringBuilder();
            if (!Validator.checkRequired(this.tbId.Text))
            {
                strErrorMsg.Append("编号不能为空\n");
            }
            else
            {
                if (PatientDao.getById(Int32.Parse(this.tbId.Text.Trim())) != null)
                {
                    strErrorMsg.Append("此编号已存在\n");
                }
                if (!Validator.checkInteger(this.tbId.Text))
                {
                    strErrorMsg.Append("编号必须为整数\n");
                }
            }
            if (this.tbBirthWeight.Text != string.Empty && !Validator.checkNumber(this.tbBirthWeight.Text))
            {
                strErrorMsg.Append("体重必须是数字\n");
            }
            if (strErrorMsg.Length > 0)
            {
                MessageBox.Show(strErrorMsg.ToString(), "出错提示");

                return;
            }

            Patient p = new Patient();

            p.Id = Int32.Parse(this.tbId.Text.Trim());

            p.Name = this.tbName.Text.Trim();
            p.Gender = this.cbGender.Text;
            p.Birthday = DateTime.Parse(this.dtBirthday.Value.ToShortDateString());
            p.Telephone = this.tbTelephone.Text.Trim();
            p.Address = this.tbAddress.Text.Trim();
            if (this.tbBirthWeight.Text != string.Empty && this.tbBirthWeight.Text != null)
            {
                p.BirthWeight = Double.Parse(this.tbBirthWeight.Text.Trim());
            }
            p.BirthSituation = this.cbBirthSituation.Text;
            p.Childbirth = this.cbChildBirth.Text;
            PatientDao.insert(p);
            this.btnAddPatient.Visible = false;
            this.btnUpdatePatient.Visible = true;
            this.hasId = true;
            MessageBox.Show("病人信息创建成功", "提示");

        }

        private void btnUpdatePatient_Click(object sender, EventArgs e)
        {
            Patient p = new Patient();

            p.Name = this.tbName.Text.Trim();
            p.Gender = this.cbGender.Text;
            p.Birthday = DateTime.Parse(this.dtBirthday.Value.ToShortDateString());
            p.Telephone = this.tbTelephone.Text.Trim();
            p.Address = this.tbAddress.Text.Trim();
            if (this.tbBirthWeight.Text != string.Empty && this.tbBirthWeight.Text != null)
            {
                p.BirthWeight = Double.Parse(this.tbBirthWeight.Text.Trim());
            }
            p.BirthSituation = this.cbBirthSituation.Text;
            p.Childbirth = this.cbChildBirth.Text;
            p.Id = Int32.Parse(this.tbId.Text.Trim());
            PatientDao.update(p);

            MessageBox.Show("病人信息更新成功", "提示");
        }

        private void Patient_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((this.tbId.Text.Trim() == string.Empty || !this.hasId) && this.Patient.SelectedIndex != 0)
            {
                this.Patient.SelectedIndex = 0;
                this.Patient.TabPages[0].Parent.Focus();
                MessageBox.Show("请先保存基本信息", "提示");
            }
            else if (this.Patient.SelectedIndex == 1)
            {
                this.tpCondition.Controls.Add(this.gbMaster);
                this.tpCondition.Controls.Add(this.gbSymptom);
                this.tpCondition.Controls.Add(this.gbExamination);
                this.lblSympScore.Visible = false;
                this.cbSympScore.Visible = false;
                Condition c = ConditionDao.getByPatientId(Int32.Parse(this.tbId.Text.Trim()));
                RefreshCondition(c);
            }
            else if (this.Patient.SelectedIndex == 2)
            {
                this.tpFirstDiagnose.Controls.Add(this.gbMaster);
                this.tpFirstDiagnose.Controls.Add(this.gbSymptom);
                this.tpFirstDiagnose.Controls.Add(this.gbExamination);
                this.tpFirstDiagnose.Controls.Add(this.gbDiagnosis);
                this.tpFirstDiagnose.Controls.Add(this.gbDrug);
                this.tpFirstDiagnose.Controls.Add(this.gbWestern);
                this.tpFirstDiagnose.Controls.Add(this.gbYGSTT);

                Diagnose d = DiagnoseDao.getFirstDiagnose(Int32.Parse(this.tbId.Text.Trim()));
                RefreshDiagnose(d, true);

            }
            else if (this.Patient.SelectedIndex == 3)
            {
                this.tpDiagnose.Controls.Add(this.gbMaster);
                this.tpDiagnose.Controls.Add(this.gbSymptom);
                this.tpDiagnose.Controls.Add(this.gbExamination);
                this.tpDiagnose.Controls.Add(this.gbDiagnosis);
                this.tpDiagnose.Controls.Add(this.gbDrug);
                this.tpDiagnose.Controls.Add(this.gbWestern);
                this.tpDiagnose.Controls.Add(this.gbYGSTT);

                RefreshDiagnose(null, false);

                this.cbShowDiagnose.Checked = false;
                this.dtMasterDate.Visible = true;
            }
            else if (this.Patient.SelectedIndex == 4)
            {
                this.tpTCM.Controls.Add(this.gbMaster);
                RefreshTCM(null);

                this.cbShowTCM.Checked = false;
                this.dtMasterDate.Visible = true;
            }
        }

        private void btnAddMaster_Click(object sender, EventArgs e)
        {

            if (this.Patient.SelectedIndex == 1)
            {

                Condition c = new Condition();
                c.PatientId = Int32.Parse(this.tbId.Text.Trim());
                c.CreateDate = DateTime.Parse(this.dtMasterDate.Value.ToShortDateString());
                c.Id = ConditionDao.insert(c);
                RefreshCondition(c);
            }
            if (this.Patient.SelectedIndex == 2 || this.Patient.SelectedIndex == 3)
            {
                StringBuilder strErrorMsg = new StringBuilder();
                if (DiagnoseDao.getDiagnoseCountByDate(Int32.Parse(this.tbId.Text.Trim()), DateTime.Parse(this.dtMasterDate.Value.ToShortDateString())) > 0)
                {
                    strErrorMsg.Append("此日期已存在\n");
                }

                if (strErrorMsg.Length > 0)
                {
                    MessageBox.Show(strErrorMsg.ToString(), "出错提示");

                    return;
                }

                Diagnose d = new Diagnose();
                d.PatientId = Int32.Parse(this.tbId.Text.Trim());
                d.CreateDate = DateTime.Parse(this.dtMasterDate.Value.ToShortDateString());
                if (this.Patient.SelectedIndex == 2)
                {
                    d.IsFirst = true;
                }
                if (this.Patient.SelectedIndex == 3)
                {
                    d.IsFirst = false;
                }
                d.Id = DiagnoseDao.insert(d);
                RefreshDiagnose(d, d.IsFirst);
            }
            if (this.Patient.SelectedIndex == 4)
            {
                StringBuilder strErrorMsg = new StringBuilder();
                if (TCMDao.getTCMCountByDate(Int32.Parse(this.tbId.Text.Trim()), DateTime.Parse(this.dtMasterDate.Value.ToShortDateString())) > 0)
                {
                    strErrorMsg.Append("此日期已存在\n");
                }

                if (strErrorMsg.Length > 0)
                {
                    MessageBox.Show(strErrorMsg.ToString(), "出错提示");

                    return;
                }

                TCM t = new TCM();
                t.PatientId = Int32.Parse(this.tbId.Text.Trim());
                t.CreateDate = DateTime.Parse(this.dtMasterDate.Value.ToShortDateString());
                t.Id = TCMDao.insert(t);
                RefreshTCM(t);
            }
            MessageBox.Show("基本信息创建成功", "提示");
        }

        private void RefreshDiagnose(Diagnose d, bool isFirst)
        {

            //基本信息
            this.btnAddMaster.Visible = d != null ? false : true;
            this.btnUpdateMaster.Visible = d != null ? true : false;
            this.tbMasterId.Text = d != null ? d.Id.ToString() : string.Empty;
            this.dtMasterDate.Visible = isFirst || (!this.cbShowDiagnose.Checked && !this.cbShowTCM.Checked);
            this.dtMasterDate.Text = d != null ? d.CreateDate.ToShortDateString() : DateTime.Now.ToShortDateString();
            this.cbDiagnoseDate.Visible = !isFirst && this.cbShowDiagnose.Checked;
            this.cbDiagnoseDate.Text = d != null ? d.CreateDate.ToShortDateString() : string.Empty;
            this.cbShowDiagnose.Visible = !isFirst;
            //this.cbShowDiagnose.Checked = false;
            this.cbTCMDate.Visible = false;
            this.cbShowTCM.Visible = false;
            //this.cbShowTCM.Checked = false;


            //按钮
            this.btnAddSymptom.Visible = true;
            this.btnUpdateSymptom.Visible = false;
            this.btnDeleteSymptom.Visible = false;

            this.btnAddExamination.Visible = true;
            this.btnUpdateExamination.Visible = false;
            this.btnDeleteExamination.Visible = false;

            this.btnAddDrug.Visible = true;
            this.btnUpdateDrug.Visible = false;
            this.btnDeleteDrug.Visible = false;

            if (d != null)
            {

                d = DiagnoseDao.getById(d.Id);
                //诊断
                this.gbDiagnosis.Visible = true;
                this.tbTongue.Text = d.Tongue;
                this.tbMoss.Text = d.Moss;
                this.tbPulse.Text = d.Pulse;
                this.cbSyndrome.Checked = d.Syndrome;
                this.cbSyndromeAdd.Text = d.SyndromeAdd;


                //症状
                this.gbSymptom.Visible = true;
                this.columnHeader2.Text = "性质";
                this.columnHeader3.Text = "复杂性";
                this.columnHeader4.Text = "部位";
                this.columnHeader5.Text = "状态";
                this.columnHeader6.Text = "分数";

                this.lvSymptom.Items.Clear();
                List<DiagnoseSymptom> dls = DiagnoseSymptomDao.getAllDiagnoseSymptoms(d.Id);

                if (dls != null)
                {
                    foreach (DiagnoseSymptom dd in dls)
                    {
                        AddDiagnoseSymptom(dd);
                    }
                }

                DataTable dtSympKind = ComboxDataSourceUtil.getSymptomKind();
                this.cbSympKind.DataSource = dtSympKind;
                this.cbSympKind.DisplayMember = "name";
                this.cbSympKind.ValueMember = "name";
                this.cbSympKind.SelectedIndex = 0;
                this.cbSympScore.SelectedIndex = 0;
                this.lblSympScore.Visible = true;
                this.cbSympScore.Visible = true;
                this.lblSympSeq.Visible = false;
                this.tbSympSeq.Visible = false;

                //检查
                this.gbExamination.Visible = true;
                this.lvExamination.Items.Clear();
                List<PatientExamination> pls = PatientExaminationDao.getAllPatientExaminations(d.Id, "Diagnose");
                if (pls != null)
                {
                    foreach (PatientExamination pe in pls)
                    {
                        AddPatientExamination(pe);
                    }
                }

                DataTable dtExamType = ComboxDataSourceUtil.getExamType();
                this.cbExamType.DataSource = dtExamType;
                this.cbExamType.DisplayMember = "name";
                this.cbExamType.ValueMember = "name";
                this.cbExamType.SelectedIndex = 0;
                this.tbExamResult.Text = string.Empty;

                //治疗
                this.gbDrug.Visible = true;
                this.lvDrug.Items.Clear();
                List<DiagnoseDrug> ddls = DiagnoseDrugDao.getAllDiagnose(d.Id);
                if (ddls != null)
                {
                    foreach (DiagnoseDrug dd in ddls)
                    {
                        AddDiagnoseDrug(dd);
                    }
                }

                DataTable dtDrug = ComboxDataSourceUtil.getDrug();
                this.cbDurg.DataSource = dtDrug;
                this.cbDurg.DisplayMember = "name";
                this.cbDurg.ValueMember = "id";
                this.cbDurg.SelectedIndex = 0;
                this.tbDrugAmount.Text = string.Empty;

                //西医治疗
                this.gbWestern.Visible = true;
                this.rbWesternNo.Checked = d.Western ? false : true;
                this.rbWesternYes.Checked = d.Western ? true : false;
                this.tbWestern.Text = d.WesternDrug;

                //耶鲁
                this.gbYGSTT.Visible = true;

                DiagnoseYGSTT dy = DiagnoseYGSTTDao.getByDiagnoseId(d.Id);
                if (dy != null)
                {
                    this.tbYDamageRate.Text = dy.DamageRate;
                    this.tbYMComplexity.Text = dy.MotorComplexity;
                    this.tbYmFrequency.Text = dy.MotorFrequency;
                    this.tbYMInterference.Text = dy.MotorInterference;
                    this.tbYMOrder.Text = dy.MotorOrder;
                    this.tbYMScore.Text = dy.MotorScore;
                    this.tbYMStrength.Text = dy.MotorStrength;
                    this.tbYPComplexity.Text = dy.PhonicComplexity;
                    this.tbYPFrequency.Text = dy.PhonicFrequency;
                    this.tbYPInterference.Text = dy.PhonicInterference;
                    this.tbYPOrder.Text = dy.PhonicOrder;
                    this.tbYPScore.Text = dy.PhonicScore;
                    this.tbYPStrength.Text = dy.PhonicStrength;
                    this.tbYSeverity.Text = dy.Severity;
                }
                else
                {
                    this.tbYDamageRate.Text = string.Empty;
                    this.tbYMComplexity.Text = string.Empty;
                    this.tbYmFrequency.Text = string.Empty;
                    this.tbYMInterference.Text = string.Empty;
                    this.tbYMOrder.Text = string.Empty;
                    this.tbYMScore.Text = string.Empty;
                    this.tbYMStrength.Text = string.Empty;
                    this.tbYPComplexity.Text = string.Empty;
                    this.tbYPFrequency.Text = string.Empty;
                    this.tbYPInterference.Text = string.Empty;
                    this.tbYPOrder.Text = string.Empty;
                    this.tbYPScore.Text = string.Empty;
                    this.tbYPStrength.Text = string.Empty;
                    this.tbYSeverity.Text = string.Empty;
                }

            }
            else
            {
                this.gbDiagnosis.Visible = false;
                this.gbSymptom.Visible = false;
                this.gbExamination.Visible = false;
                this.gbDrug.Visible = false;
                this.gbWestern.Visible = false;
                this.gbYGSTT.Visible = false;
            }

        }

        private void RefreshTCM(TCM t)
        {

            //基本信息
            this.btnAddMaster.Visible = t == null ? true : false;
            this.btnUpdateMaster.Visible = t == null ? false : true;
            this.tbMasterId.Text = t == null ? string.Empty : t.Id.ToString();
            this.dtMasterDate.Visible = !this.cbShowDiagnose.Checked && !this.cbShowTCM.Checked;
            this.dtMasterDate.Text = t == null ? DateTime.Now.ToShortDateString() : t.CreateDate.ToShortDateString();
            this.cbDiagnoseDate.Visible = false;
            this.cbTCMDate.Visible = this.cbShowTCM.Checked;
            this.cbTCMDate.Text = t != null ? t.CreateDate.ToShortDateString() : string.Empty;
            // this.cbShowDiagnose.Visible = false;
            this.cbShowTCM.Visible = true;
            //this.cbShowTCM.Checked = false;

            if (t != null)
            {
                t = TCMDao.getById(t.Id);
                //证候
                this.gbTCM.Visible = true;
                this.cbSyndrome1.Text = t.Syndrome1;
                this.cbSyndrome2.Text = t.Syndrome2;
                this.cbSyndrome3.Text = t.Syndrome3;
                this.cbSyndrome4.Text = t.Syndrome4;
                this.cbSyndrome5.Text = t.Syndrome5;
                this.cbSyndrome6.Text = t.Syndrome6;
                this.cbSyndrome7.Text = t.Syndrome7;
                this.cbSyndrome8.Text = t.Syndrome8;
                this.cbSyndrome9.Text = t.Syndrome9;
                this.cbSyndrome10.Text = t.Syndrome10;
                this.cbSyndrome11.Text = t.Syndrome11;
                this.cbSyndrome12.Text = t.Syndrome12;
                this.cbSyndrome13.Text = t.Syndrome13;
                this.cbSyndrome14.Text = t.Syndrome14;
                this.cbSyndrome15.Text = t.Syndrome15;
                this.cbSyndrome16.Text = t.Syndrome16;
                this.cbSyndrome17.Text = t.Syndrome17;
                this.cbSyndrome18.Text = t.Syndrome18;
                this.cbSyndrome19.Text = t.Syndrome19;
                this.cbSyndrome20.Text = t.Syndrome20;
                this.cbSyndrome21.Text = t.Syndrome21;
                this.cbSyndrome22.Text = t.Syndrome22;
                this.cbSyndrome23.Text = t.Syndrome23;
                this.cbSyndrome24.Text = t.Syndrome24;
                this.cbSyndrome25.Text = t.Syndrome25;
                this.cbSyndrome26.Text = t.Syndrome26;
                this.cbSyndrome27.Text = t.Syndrome27;
                this.cbSyndrome28.Text = t.Syndrome28;
                this.cbSyndrome29.Text = t.Syndrome29;
                this.cbSyndrome30.Text = t.Syndrome30;
                //舌脉
                this.gbLingualVein.Visible = true;
                if (t.Tongue1 != null && t.Tongue1 != string.Empty)
                {
                    string[] tougue1 = (string[])t.Tongue1.Split('-');
                    this.cbTongue11.Checked = bool.Parse(tougue1[0]);
                    this.cbTongue12.Checked = bool.Parse(tougue1[1]);
                    this.cbTongue13.Checked = bool.Parse(tougue1[2]);
                    this.cbTongue14.Checked = bool.Parse(tougue1[3]);
                    this.cbTongue15.Checked = bool.Parse(tougue1[4]);

                }
                if (t.Tongue2 != null && t.Tongue2 != string.Empty)
                {
                    string[] tougue2 = (string[])t.Tongue2.Split('-');
                    this.cbTongue21.Checked = bool.Parse(tougue2[0]);
                    this.cbTongue22.Checked = bool.Parse(tougue2[1]);
                    this.cbTongue23.Checked = bool.Parse(tougue2[2]);
                    this.cbTongue24.Checked = bool.Parse(tougue2[3]);
                    this.cbTongue25.Checked = bool.Parse(tougue2[4]);
                    this.cbTongue26.Checked = bool.Parse(tougue2[5]);

                }
                if (t.Tongue3 != null && t.Tongue3 != string.Empty)
                {
                    string[] tougue3 = (string[])t.Tongue3.Split('-');
                    this.cbTongue31.Checked = bool.Parse(tougue3[0]);
                    this.cbTongue32.Checked = bool.Parse(tougue3[1]);
                    this.cbTongue33.Checked = bool.Parse(tougue3[2]);
                    this.cbTongue34.Checked = bool.Parse(tougue3[3]);
                    this.cbTongue35.Checked = bool.Parse(tougue3[4]);
                    this.cbTongue36.Checked = bool.Parse(tougue3[5]);
                    this.cbTongue37.Checked = bool.Parse(tougue3[6]);
                    this.cbTongue38.Checked = bool.Parse(tougue3[7]);
                    this.cbTongue39.Checked = bool.Parse(tougue3[8]);
                    this.cbTongue310.Checked = bool.Parse(tougue3[9]);
                }
                if (t.Pulse != null && t.Pulse != string.Empty)
                {
                    string[] pulse = (string[])t.Pulse.Split('-');
                    this.cbPulse1.Checked = bool.Parse(pulse[0]);
                    this.cbPulse2.Checked = bool.Parse(pulse[1]);
                    this.cbPulse3.Checked = bool.Parse(pulse[2]);
                    this.cbPulse4.Checked = bool.Parse(pulse[3]);
                    this.cbPulse5.Checked = bool.Parse(pulse[4]);
                    this.cbPulse6.Checked = bool.Parse(pulse[5]);
                    this.cbPulse7.Checked = bool.Parse(pulse[6]);
                    this.cbPulse8.Checked = bool.Parse(pulse[7]);
                    this.cbPulse9.Checked = bool.Parse(pulse[8]);
                    this.cbPulse10.Checked = bool.Parse(pulse[9]);
                    this.cbPulse11.Checked = bool.Parse(pulse[10]);
                    this.cbPulse12.Checked = bool.Parse(pulse[11]);
                    this.cbPulse13.Checked = bool.Parse(pulse[12]);
                    this.cbPulse14.Checked = bool.Parse(pulse[13]);
                    this.cbPulse15.Checked = bool.Parse(pulse[14]);
                }
                //结论
                this.gbConclusion.Visible = true;
                this.tbDifferentiation.Text = t.Differentiation;
                this.tbConclusion.Text = t.Conclusion;
            }
            else
            {
                this.gbTCM.Visible = false;
                this.gbLingualVein.Visible = false;
                this.gbConclusion.Visible = false;
            }
        }

        private void RefreshCondition(Condition c)
        {

            //基本信息
            this.btnAddMaster.Visible = c != null ? false : true;
            this.btnUpdateMaster.Visible = c != null ? true : false;
            this.tbMasterId.Text = c != null ? c.Id.ToString() : string.Empty;
            this.dtMasterDate.Text = c != null ? c.CreateDate.ToShortDateString() : DateTime.Now.ToShortDateString();
            this.cbDiagnoseDate.Visible = false;
            this.cbShowDiagnose.Visible = false;
            this.cbTCMDate.Visible = false;
            this.cbShowTCM.Visible = false;
            this.cbShowTCM.Checked = false;

            //按钮
            this.btnAddSymptom.Visible = true;
            this.btnUpdateSymptom.Visible = false;
            this.btnDeleteSymptom.Visible = false;

            this.btnAddExamination.Visible = true;
            this.btnUpdateExamination.Visible = false;
            this.btnDeleteExamination.Visible = false;


            if (c != null)
            {
                //诱发因素
                this.gbConditionFactor.Visible = true;
                showFactors(c.HasFactor);
                setFactor6Description(c.Factor6);
                this.rbFactorNo.Checked = c.HasFactor ? false : true;
                this.rbFactorYes.Checked = c.HasFactor ? true : false;
                this.cbFactor1.Checked = c.Factor1;
                this.cbFactor2.Checked = c.Factor2;
                this.cbFactor3.Checked = c.Factor3;
                this.cbFactor4.Checked = c.Factor4;
                this.cbFactor5.Checked = c.Factor5;
                this.cbFactor6.Checked = c.Factor6;
                this.tbFactor6Desc.Text = c.Factor6Desc;

                //症状
                this.gbSymptom.Visible = true;
                this.columnHeader2.Text = "序号";
                this.columnHeader3.Text = "性质";
                this.columnHeader4.Text = "复杂性";
                this.columnHeader5.Text = "部位";
                this.columnHeader6.Text = "状态";
                this.lvSymptom.Items.Clear();
                List<ConditionSymptom> ls = ConditionSymptomDao.getAllConditionSymptoms(c.Id);
                if (ls != null)
                {
                    foreach (ConditionSymptom cd in ls)
                    {
                        AddConditionSymptom(cd);
                    }
                }



                DataTable dtSympKind = ComboxDataSourceUtil.getSymptomKind();
                this.cbSympKind.DataSource = dtSympKind;
                this.cbSympKind.DisplayMember = "name";
                this.cbSympKind.ValueMember = "name";
                this.cbSympKind.SelectedIndex = 0;
                this.tbSympSeq.Text = string.Empty;
                this.lblSympScore.Visible = false;
                this.cbSympScore.Visible = false;
                this.lblSympSeq.Visible = true;
                this.tbSympSeq.Visible = true;



                //检查
                this.gbExamination.Visible = true;
                this.lvExamination.Items.Clear();
                List<PatientExamination> pls = PatientExaminationDao.getAllPatientExaminations(c.Id, "Condition");
                if (pls != null)
                {
                    foreach (PatientExamination pe in pls)
                    {
                        AddPatientExamination(pe);
                    }
                }

                DataTable dtExamType = ComboxDataSourceUtil.getExamType();
                this.cbExamType.DataSource = dtExamType;
                this.cbExamType.DisplayMember = "name";
                this.cbExamType.ValueMember = "name";
                this.cbExamType.SelectedIndex = 0;
                this.tbExamResult.Text = string.Empty;

            }
            else
            {
                this.gbConditionFactor.Visible = false;
                this.gbSymptom.Visible = false;
                this.gbExamination.Visible = false;
            }

        }

        private void AddConditionSymptom(ConditionSymptom cd)
        {
            ListViewItem lvi = this.lvSymptom.Items.Add(cd.Id.ToString());
            lvi.SubItems.Add(cd.Sequence.ToString());
            lvi.SubItems.Add(cd.Kind);
            lvi.SubItems.Add(cd.Complexity);
            lvi.SubItems.Add(cd.Part);
            lvi.SubItems.Add(cd.State);
        }

        private void AddDiagnoseSymptom(DiagnoseSymptom dd)
        {
            ListViewItem lvi = this.lvSymptom.Items.Add(dd.Id.ToString());
            lvi.SubItems.Add(dd.Kind);
            lvi.SubItems.Add(dd.Complexity);
            lvi.SubItems.Add(dd.Part);
            lvi.SubItems.Add(dd.State);
            lvi.SubItems.Add(dd.Result);
        }

        private void AddPatientExamination(PatientExamination pe)
        {
            ListViewItem lvi = this.lvExamination.Items.Add(pe.Id.ToString());
            lvi.SubItems.Add(pe.ExaminationType);
            lvi.SubItems.Add(pe.ExaminationItem);
            lvi.SubItems.Add(pe.Result.ToString());
        }

        private void AddDiagnoseDrug(DiagnoseDrug dd)
        {
            ListViewItem lvi = this.lvDrug.Items.Add(dd.Id.ToString());
            lvi.SubItems.Add(dd.Name);
            lvi.SubItems.Add(dd.Amount.ToString());
            lvi.SubItems.Add(dd.Uom.ToString());
        }


        private void btnUpdateMaster_Click(object sender, EventArgs e)
        {
            if (this.Patient.SelectedIndex == 1)
            {
                Condition c = new Condition();
                c.CreateDate = DateTime.Parse(this.dtMasterDate.Value.ToShortDateString());
                c.Id = Int32.Parse(this.tbMasterId.Text.Trim());
                ConditionDao.update(c, false);
            }
            if (this.Patient.SelectedIndex == 2 || this.Patient.SelectedIndex == 3)
            {

                Diagnose d = DiagnoseDao.getById(Int32.Parse(this.tbMasterId.Text.Trim()));
                StringBuilder strErrorMsg = new StringBuilder();
                if (DateTime.Parse(this.dtMasterDate.Value.ToShortDateString()) != d.CreateDate && DiagnoseDao.getDiagnoseCountByDate(Int32.Parse(this.tbId.Text.Trim()), DateTime.Parse(this.dtMasterDate.Value.ToShortDateString())) > 0)
                {
                    strErrorMsg.Append("此日期已存在\n");
                }

                if (strErrorMsg.Length > 0)
                {
                    MessageBox.Show(strErrorMsg.ToString(), "出错提示");

                    return;
                }

                d.CreateDate = DateTime.Parse(this.dtMasterDate.Value.ToShortDateString());
                DiagnoseDao.update(d, false, false);
            }
            if (this.Patient.SelectedIndex == 4)
            {
                TCM t = TCMDao.getById(Int32.Parse(this.tbMasterId.Text.Trim()));
                StringBuilder strErrorMsg = new StringBuilder();
                if (DateTime.Parse(this.dtMasterDate.Value.ToShortDateString()) != t.CreateDate && TCMDao.getTCMCountByDate(Int32.Parse(this.tbId.Text.Trim()), DateTime.Parse(this.dtMasterDate.Value.ToShortDateString())) > 0)
                {
                    strErrorMsg.Append("此日期已存在\n");
                }

                if (strErrorMsg.Length > 0)
                {
                    MessageBox.Show(strErrorMsg.ToString(), "出错提示");

                    return;
                }

                t.CreateDate = DateTime.Parse(this.dtMasterDate.Value.ToShortDateString());
                TCMDao.update(t);
            }
            MessageBox.Show("基本信息更新成功", "提示");
        }

        private void rbFactorYes_CheckedChanged(object sender, EventArgs e)
        {
            showFactors(true);
        }

        private void rbFactorNo_CheckedChanged(object sender, EventArgs e)
        {
            showFactors(false);
        }

        private void showFactors(bool showFactors)
        {
            this.cbFactor1.Visible = showFactors;
            this.cbFactor2.Visible = showFactors;
            this.cbFactor3.Visible = showFactors;
            this.cbFactor4.Visible = showFactors;
            this.cbFactor5.Visible = showFactors;
            this.cbFactor6.Visible = showFactors;
            this.tbFactor6Desc.Visible = showFactors;
            if (!showFactors)
            {
                this.cbFactor1.Checked = showFactors;
                this.cbFactor2.Checked = showFactors;
                this.cbFactor3.Checked = showFactors;
                this.cbFactor4.Checked = showFactors;
                this.cbFactor5.Checked = showFactors;
                this.cbFactor6.Checked = showFactors;
                this.tbFactor6Desc.Text = string.Empty;
            }
        }
        private void cbFactor6_CheckedChanged(object sender, EventArgs e)
        {
            setFactor6Description(this.cbFactor6.Checked);
        }

        private void setFactor6Description(bool hasChecked)
        {
            if (hasChecked)
            {
                this.tbFactor6Desc.ReadOnly = false;
            }
            else
            {
                this.tbFactor6Desc.ReadOnly = true;
                this.tbFactor6Desc.Text = string.Empty;

            }
        }

        private void btnUpdateFactor_Click(object sender, EventArgs e)
        {
            StringBuilder strErrorMsg = new StringBuilder();
            if (this.rbFactorYes.Checked && !this.cbFactor1.Checked && !this.cbFactor2.Checked && !this.cbFactor3.Checked && !this.cbFactor4.Checked && !this.cbFactor5.Checked && !this.cbFactor6.Checked)
            {
                MessageBox.Show("请选择诱发因素", "出错提示");
                return;
            }

            Condition c = new Condition();
            c.HasFactor = this.rbFactorYes.Checked;
            c.Factor1 = this.cbFactor1.Checked;
            c.Factor2 = this.cbFactor2.Checked;
            c.Factor3 = this.cbFactor3.Checked;
            c.Factor4 = this.cbFactor4.Checked;
            c.Factor5 = this.cbFactor5.Checked;
            c.Factor6 = this.cbFactor6.Checked;
            c.Factor6Desc = this.tbFactor6Desc.Text;

            c.Id = Int32.Parse(this.tbMasterId.Text.Trim());
            ConditionDao.update(c, true);
            MessageBox.Show("诱发因素更新成功", "提示");
        }

        private void cbSyndrome_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbSyndrome.Checked)
            {
                this.cbSyndromeAdd.Visible = true;
            }
            else
            {
                this.cbSyndromeAdd.Visible = false;
                this.cbSyndromeAdd.Text = string.Empty;
            }
        }

        private void cbShowDiagnose_CheckedChanged(object sender, EventArgs e)
        {

            this.gbSymptom.Visible = false;
            this.gbExamination.Visible = false;
            this.gbDrug.Visible = false;
            this.gbWestern.Visible = false;
            this.gbDiagnosis.Visible = false;
            this.gbYGSTT.Visible = false;
            if (this.cbShowDiagnose.Checked)
            {
                this.dtMasterDate.Visible = false;
                this.cbDiagnoseDate.Visible = true;
                this.btnAddMaster.Visible = false;
                this.btnUpdateMaster.Visible = false;


                DataTable dtDiagnoseDate = ComboxDataSourceUtil.getFollowDiagnoses(int.Parse(this.tbId.Text));
                this.cbDiagnoseDate.DataSource = dtDiagnoseDate;
                this.cbDiagnoseDate.DisplayMember = "create_date";
                this.cbDiagnoseDate.ValueMember = "id";


                //选择后直接显示最新一条记录
                if (this.Patient.SelectedIndex == 3 && this.cbDiagnoseDate.SelectedValue != null)
                {

                    Diagnose d = DiagnoseDao.getById(Int32.Parse(this.cbDiagnoseDate.SelectedValue.ToString()));
                    RefreshDiagnose(d, false);
                    this.btnAddMaster.Visible = false;
                    this.btnUpdateMaster.Visible = false;
                }

            }
            else
            {
                this.dtMasterDate.Visible = true;
                this.cbDiagnoseDate.Visible = false;

                this.btnAddMaster.Visible = true;
                this.btnUpdateMaster.Visible = false;

                this.dtMasterDate.Text = DateTime.Now.ToShortDateString();
            }
        }



        private void rbWesternNo_CheckedChanged(object sender, EventArgs e)
        {
            showWestern(false);
        }

        private void rbWesternYes_CheckedChanged(object sender, EventArgs e)
        {
            showWestern(true);
        }

        private void showWestern(bool showWestern)
        {
            this.tbWestern.Visible = showWestern;
            this.tbWestern.Text = showWestern ? this.tbWestern.Text : string.Empty;
        }

        private void btnUpdateDiagnosis_Click(object sender, EventArgs e)
        {
            Diagnose d = new Diagnose();
            d.Tongue = this.tbTongue.Text;
            d.Moss = this.tbMoss.Text;
            d.Pulse = this.tbPulse.Text;
            d.Syndrome = this.cbSyndrome.Checked;
            d.SyndromeAdd = this.cbSyndromeAdd.Text;
            d.Id = Int32.Parse(this.tbMasterId.Text.Trim());
            DiagnoseDao.update(d, true, false);
            MessageBox.Show("诊断情况更新成功", "提示");
        }

        private void btnUpdateWestern_Click(object sender, EventArgs e)
        {
            Diagnose d = new Diagnose();
            d.Western = this.rbWesternYes.Checked;
            d.WesternDrug = this.tbWestern.Text;
            d.Id = Int32.Parse(this.tbMasterId.Text.Trim());
            DiagnoseDao.update(d, false, true);
            MessageBox.Show("西医用药情况更新成功", "提示");
        }

        private void btnAddSymptom_Click(object sender, EventArgs e)
        {
            StringBuilder strErrorMsg = new StringBuilder();
            if (this.Patient.SelectedIndex == 1)
            {
                if (!Validator.checkRequired(this.tbSympSeq.Text))
                {
                    strErrorMsg.Append("序号不能为空\n");
                }
                else
                {
                    if (!Validator.checkInteger(this.tbSympSeq.Text))
                    {
                        strErrorMsg.Append("序号必须为整数");
                    }
                    if (ConditionSymptomDao.getCountBySeq(Int32.Parse(this.tbMasterId.Text), Int32.Parse(this.tbSympSeq.Text.Trim())) > 0)
                    {
                        strErrorMsg.Append("此序号已存在\n");
                    }
                    if (ConditionSymptomDao.getCountBySymptomId(Int32.Parse(this.tbMasterId.Text), Int32.Parse(this.cbSympState.SelectedValue.ToString())) > 0)
                    {
                        strErrorMsg.Append("此症状已存在\n");
                    }
                }
                if (strErrorMsg.Length > 0)
                {
                    MessageBox.Show(strErrorMsg.ToString(), "出错提示");

                    return;
                }
                ConditionSymptom cs = new ConditionSymptom();
                cs.MstrId = Int32.Parse(this.tbMasterId.Text);
                cs.SymptomId = Int32.Parse(this.cbSympState.SelectedValue.ToString());
                cs.Sequence = Int32.Parse(this.tbSympSeq.Text);
                int id = ConditionSymptomDao.insert(cs);
                cs = ConditionSymptomDao.getById(id);
                AddConditionSymptom(cs);
                this.tbSympSeq.Text = string.Empty;
            }
            if (this.Patient.SelectedIndex == 2 || this.Patient.SelectedIndex == 3)
            {

                if (DiagnoseSymptomDao.getCountBySymptomId(Int32.Parse(this.tbMasterId.Text), Int32.Parse(this.cbSympState.SelectedValue.ToString())) > 0)
                {
                    strErrorMsg.Append("此症状已存在\n");
                }

                if (strErrorMsg.Length > 0)
                {
                    MessageBox.Show(strErrorMsg.ToString(), "出错提示");
                    return;
                }

                DiagnoseSymptom ds = new DiagnoseSymptom();
                ds.MstrId = Int32.Parse(this.tbMasterId.Text);
                ds.SymptomId = Int32.Parse(this.cbSympState.SelectedValue.ToString());
                ds.Result = this.cbSympScore.Text;
                int id = DiagnoseSymptomDao.insert(ds);
                ds = DiagnoseSymptomDao.getById(id);
                AddDiagnoseSymptom(ds);
                this.cbSympScore.SelectedIndex = 0;
            }

            this.cbSympKind.SelectedIndex = 0;

            // MessageBox.Show("症状添加成功", "提示");
        }

        private void btnUpdateSymptom_Click(object sender, EventArgs e)
        {
            StringBuilder strErrorMsg = new StringBuilder();
            if (this.Patient.SelectedIndex == 1)
            {
                if (!Validator.checkRequired(this.tbSympSeq.Text))
                {
                    strErrorMsg.Append("序号不能为空\n");
                }
                else
                {
                    if (!Validator.checkInteger(this.tbSympSeq.Text))
                    {
                        strErrorMsg.Append("序号必须为整数");
                    }
                    if (ConditionSymptomDao.getCountBySeq(Int32.Parse(this.tbMasterId.Text), Int32.Parse(this.tbSympSeq.Text.Trim())) > 1)
                    {
                        strErrorMsg.Append("此序号已存在\n");
                    }
                    if (ConditionSymptomDao.getCountBySymptomId(Int32.Parse(this.tbMasterId.Text), Int32.Parse(this.cbSympState.SelectedValue.ToString())) > 1)
                    {
                        strErrorMsg.Append("此症状已存在\n");
                    }
                }
                if (strErrorMsg.Length > 0)
                {
                    MessageBox.Show(strErrorMsg.ToString(), "出错提示");

                    return;
                }

                ConditionSymptom cs = new ConditionSymptom();
                cs.SymptomId = Int32.Parse(this.cbSympState.SelectedValue.ToString());
                ListViewItem ls = this.lvSymptom.SelectedItems[0];
                cs.Id = Int32.Parse(ls.SubItems[0].Text);
                cs.Sequence = Int32.Parse(this.tbSympSeq.Text);
                ConditionSymptomDao.update(cs);
                cs = ConditionSymptomDao.getById(Int32.Parse(ls.SubItems[0].Text));
                ls.SubItems[1].Text = cs.Sequence.ToString();
                ls.SubItems[2].Text = cs.Kind;
                ls.SubItems[3].Text = cs.Complexity;
                ls.SubItems[4].Text = cs.Part;
                ls.SubItems[5].Text = cs.State;

                this.tbSympSeq.Text = string.Empty;

            }
            if (this.Patient.SelectedIndex == 2 || this.Patient.SelectedIndex == 3)
            {
                if (DiagnoseSymptomDao.getCountBySymptomId(Int32.Parse(this.tbMasterId.Text), Int32.Parse(this.cbSympState.SelectedValue.ToString())) > 1)
                {
                    strErrorMsg.Append("此症状已存在\n");
                }

                if (strErrorMsg.Length > 0)
                {
                    MessageBox.Show(strErrorMsg.ToString(), "出错提示");
                    return;
                }
                DiagnoseSymptom ds = new DiagnoseSymptom();
                ds.SymptomId = Int32.Parse(this.cbSympState.SelectedValue.ToString());
                ListViewItem ls = this.lvSymptom.SelectedItems[0];
                ds.Id = Int32.Parse(ls.SubItems[0].Text);
                ds.Result = this.cbSympScore.Text;
                DiagnoseSymptomDao.update(ds);

                ds = DiagnoseSymptomDao.getById(Int32.Parse(ls.SubItems[0].Text));
                ls.SubItems[1].Text = ds.Kind;
                ls.SubItems[2].Text = ds.Complexity;
                ls.SubItems[3].Text = ds.Part;
                ls.SubItems[4].Text = ds.State;
                ls.SubItems[5].Text = ds.Result;

                this.cbSympScore.SelectedIndex = 0;

            }

            this.cbSympKind.SelectedIndex = 0;
            this.btnAddSymptom.Visible = true;
            this.btnUpdateSymptom.Visible = false;
            this.btnDeleteSymptom.Visible = false;
            //   MessageBox.Show("症状更新成功", "提示");

        }

        private void btnDeleteSymptom_Click(object sender, EventArgs e)
        {
            if (this.lvSymptom.SelectedItems.Count < 1)
            {
                MessageBox.Show("请选择要删除的记录", "提示");
                return;
            }
            ListViewItem ls = this.lvSymptom.SelectedItems[0];
            if (this.Patient.SelectedIndex == 1)
            {
                ConditionSymptomDao.deleteById(Int32.Parse(ls.SubItems[0].Text));
                this.tbSympSeq.Text = string.Empty;

            }
            if (this.Patient.SelectedIndex == 2 || this.Patient.SelectedIndex == 3)
            {
                DiagnoseSymptomDao.deleteById(Int32.Parse(ls.SubItems[0].Text));
                this.cbSympScore.SelectedIndex = 0;
            }
            ls.Remove();
            this.cbSympKind.SelectedIndex = 0;

            this.btnAddSymptom.Visible = true;
            this.btnUpdateSymptom.Visible = false;
            this.btnDeleteSymptom.Visible = false;
            //  MessageBox.Show("症状删除成功", "提示");
        }



        private void btnUpdateExamination_Click(object sender, EventArgs e)
        {
            string type = string.Empty;

            if (this.Patient.SelectedIndex == 1)
            {
                type = "Condition";
            }
            if (this.Patient.SelectedIndex == 2 || this.Patient.SelectedIndex == 3)
            {
                type = "Diagnose";
            }
            StringBuilder strErrorMsg = new StringBuilder();
            if (!Validator.checkRequired(this.tbExamResult.Text))
            {
                strErrorMsg.Append("检查结果不能为空\n");
            }
            if (PatientExaminationDao.getCountByExaminationId(Int32.Parse(this.tbMasterId.Text), Int32.Parse(this.cbExamItem.SelectedValue.ToString()), type) > 1)
            {
                strErrorMsg.Append("此检查结果已存在\n");
            }

            if (strErrorMsg.Length > 0)
            {
                MessageBox.Show(strErrorMsg.ToString(), "出错提示");
                return;
            }
            ListViewItem ls = this.lvExamination.SelectedItems[0];
            PatientExamination pe = new PatientExamination();
            pe.Id = Int32.Parse(ls.SubItems[0].Text);
            pe.ExaminationId = Int32.Parse(this.cbExamItem.SelectedValue.ToString());
            pe.Result = this.tbExamResult.Text;
            PatientExaminationDao.update(pe);


            pe = PatientExaminationDao.getById(Int32.Parse(ls.SubItems[0].Text));
            ls.SubItems[1].Text = pe.ExaminationType;
            ls.SubItems[2].Text = pe.ExaminationItem;
            ls.SubItems[3].Text = pe.Result;

            this.btnAddExamination.Visible = true;
            this.btnUpdateExamination.Visible = false;
            this.btnDeleteExamination.Visible = false;
            this.cbExamType.SelectedIndex = 0;
            this.tbExamResult.Text = string.Empty;
            //   MessageBox.Show("检查结果更新成功", "提示");
        }

        private void btnDeleteExamination_Click(object sender, EventArgs e)
        {
            ListViewItem ls = this.lvExamination.SelectedItems[0];
            PatientExaminationDao.deleteById(Int32.Parse(ls.SubItems[0].Text));
            ls.Remove();
            this.cbExamType.SelectedIndex = 0;
            this.tbExamResult.Text = string.Empty;
            this.btnAddExamination.Visible = true;
            this.btnUpdateExamination.Visible = false;
            this.btnDeleteExamination.Visible = false;
            this.cbExamType.SelectedIndex = 0;
            this.tbExamResult.Text = string.Empty;
            //  MessageBox.Show("检查结果删除成功", "提示");
        }

        private void btnAddExamination_Click(object sender, EventArgs e)
        {
            string type = string.Empty;

            if (this.Patient.SelectedIndex == 1)
            {
                type = "Condition";
            }
            if (this.Patient.SelectedIndex == 2 || this.Patient.SelectedIndex == 3)
            {
                type = "Diagnose";
            }
            StringBuilder strErrorMsg = new StringBuilder();
            if (!Validator.checkRequired(this.tbExamResult.Text))
            {
                strErrorMsg.Append("检查结果不能为空\n");
            }
            if (PatientExaminationDao.getCountByExaminationId(Int32.Parse(this.tbMasterId.Text), Int32.Parse(this.cbExamItem.SelectedValue.ToString()), type) > 0)
            {
                strErrorMsg.Append("此检查结果已存在\n");
            }

            if (strErrorMsg.Length > 0)
            {
                MessageBox.Show(strErrorMsg.ToString(), "出错提示");
                return;
            }

            PatientExamination pe = new PatientExamination();
            pe.MstrId = Int32.Parse(this.tbMasterId.Text);
            pe.ExaminationId = Int32.Parse(this.cbExamItem.SelectedValue.ToString());
            pe.Result = this.tbExamResult.Text;

            pe.Type = type;


            int id = PatientExaminationDao.insert(pe);
            pe = PatientExaminationDao.getById(id);
            AddPatientExamination(pe);
            this.cbExamType.SelectedIndex = 0;
            this.tbExamResult.Text = string.Empty;
            //  MessageBox.Show("检查结果添加成功", "提示");
        }

        private void btnUpdateDrug_Click(object sender, EventArgs e)
        {

            ListViewItem ls = this.lvDrug.SelectedItems[0];

            DiagnoseDrug dd = new DiagnoseDrug();
            dd.Id = Int32.Parse(ls.SubItems[0].Text);
            dd.DrugId = Int32.Parse(this.cbDurg.SelectedValue.ToString());
            dd.Amount = Double.Parse(this.tbDrugAmount.Text.ToString());
            DiagnoseDrugDao.update(dd);

            dd = DiagnoseDrugDao.getById(Int32.Parse(ls.SubItems[0].Text));
            ls.SubItems[1].Text = dd.Name;
            ls.SubItems[2].Text = dd.Amount.ToString();
            ls.SubItems[3].Text = dd.Uom;

            this.btnAddDrug.Visible = true;
            this.btnUpdateDrug.Visible = false;
            this.btnDeleteDrug.Visible = false;

            this.cbDurg.SelectedIndex = 0;
            this.tbDrugAmount.Text = string.Empty;

            //   MessageBox.Show("用药情况更新成功", "提示");
        }

        private void btnDeleteDrug_Click(object sender, EventArgs e)
        {
            ListViewItem ls = this.lvDrug.SelectedItems[0];
            DiagnoseDrugDao.deleteById(Int32.Parse(ls.SubItems[0].Text));

            ls.Remove();
            this.cbDurg.SelectedIndex = 0;
            this.tbDrugAmount.Text = string.Empty;
            this.btnAddDrug.Visible = true;
            this.btnUpdateDrug.Visible = false;
            this.btnDeleteDrug.Visible = false;
            this.cbDurg.SelectedIndex = 0;
            this.tbDrugAmount.Text = string.Empty;
            //  MessageBox.Show("用药情况删除成功", "提示");
        }

        private void btnAddDrug_Click(object sender, EventArgs e)
        {
            StringBuilder strErrorMsg = new StringBuilder();
            if (!Validator.checkRequired(this.tbDrugAmount.Text))
            {
                strErrorMsg.Append("用药数量不能为空\n");
            }
            else if (!Validator.checkNumber(this.tbDrugAmount.Text))
            {
                strErrorMsg.Append("用药数量必须为数字\n");
            }
            if (DiagnoseDrugDao.getCountByDrugId(Int32.Parse(this.tbMasterId.Text), Int32.Parse(this.cbDurg.SelectedValue.ToString())) > 0)
            {
                strErrorMsg.Append("此用药已存在\n");
            }

            if (strErrorMsg.Length > 0)
            {
                MessageBox.Show(strErrorMsg.ToString(), "出错提示");
                return;
            }

            DiagnoseDrug dd = new DiagnoseDrug();
            dd.DrugId = Int32.Parse(this.cbDurg.SelectedValue.ToString());
            dd.DiagnoseId = Int32.Parse(this.tbMasterId.Text.ToString());
            dd.Amount = Double.Parse(this.tbDrugAmount.Text.ToString());

            int id = DiagnoseDrugDao.insert(dd);
            dd = DiagnoseDrugDao.getById(id);
            AddDiagnoseDrug(dd);
            this.cbDurg.SelectedIndex = 0;
            this.tbDrugAmount.Text = string.Empty;
            //  MessageBox.Show("用药情况添加成功", "提示");
        }

        private void cbSympKind_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbSympKind.SelectedIndex > -1)
            {
                DataTable dtComplexity = ComboxDataSourceUtil.getSympComplexity(this.cbSympKind.Text);
                this.cbSympComplexity.DataSource = dtComplexity;
                this.cbSympComplexity.DisplayMember = "name";
                this.cbSympComplexity.ValueMember = "name";
            }
        }

        private void cbSympComplexity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbSympComplexity.SelectedIndex > -1)
            {
                string kind = ((DataRowView)this.cbSympKind.SelectedItem).Row["name"].ToString();
                string complexity = ((DataRowView)this.cbSympComplexity.SelectedItem).Row["name"].ToString();
                DataTable dtPart = ComboxDataSourceUtil.getSympPart(kind, complexity);
                this.cbSympPart.DataSource = dtPart;
                this.cbSympPart.DisplayMember = "name";
                this.cbSympPart.ValueMember = "name";
            }
        }

        private void cbSympPart_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.cbSympPart.SelectedIndex > -1)
            {
                string kind = ((DataRowView)this.cbSympKind.SelectedItem).Row["name"].ToString();
                string complexity = ((DataRowView)this.cbSympComplexity.SelectedItem).Row["name"].ToString();
                string part = ((DataRowView)this.cbSympPart.SelectedItem).Row["name"].ToString();
                DataTable dtSymptom = ComboxDataSourceUtil.getSymptom(kind, complexity, part);
                this.cbSympState.DataSource = dtSymptom;
                this.cbSympState.DisplayMember = "state";
                this.cbSympState.ValueMember = "id";
            }
        }

        private void lvSymptom_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnAddSymptom.Visible = false;
            this.btnUpdateSymptom.Visible = true;
            this.btnDeleteSymptom.Visible = true;
            if (this.Patient.SelectedIndex == 1)
            {
                if (this.lvSymptom.SelectedItems.Count > 0)
                {
                    ListViewItem ls = this.lvSymptom.SelectedItems[0];

                    int id = Int32.Parse(ls.SubItems[0].Text);
                    ConditionSymptom cs = ConditionSymptomDao.getById(id);
                    this.cbSympKind.Text = cs.Kind;
                    this.cbSympComplexity.Text = cs.Complexity;
                    this.cbSympPart.Text = cs.Part;
                    this.cbSympState.Text = cs.State;
                    this.tbSympSeq.Text = cs.Sequence.ToString();
                }
            }
            else if (this.Patient.SelectedIndex == 2 || this.Patient.SelectedIndex == 3)
            {

                if (this.lvSymptom.SelectedItems.Count > 0)
                {
                    ListViewItem ls = this.lvSymptom.SelectedItems[0];

                    int id = Int32.Parse(ls.SubItems[0].Text);
                    DiagnoseSymptom ds = DiagnoseSymptomDao.getById(id);
                    this.cbSympKind.Text = ds.Kind;
                    this.cbSympComplexity.Text = ds.Complexity;
                    this.cbSympPart.Text = ds.Part;
                    this.cbSympState.Text = ds.State;
                    this.cbSympScore.Text = ds.Result;
                }
            }
        }
        private void cbExamType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbExamType.SelectedIndex > -1)
            {
                string type = ((DataRowView)this.cbExamType.SelectedItem).Row["name"].ToString();
                DataTable dtExamination = ComboxDataSourceUtil.getExamItem(type);
                this.cbExamItem.DataSource = dtExamination;
                this.cbExamItem.DisplayMember = "item";
                this.cbExamItem.ValueMember = "id";
            }
        }

        private void lvExamination_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnAddExamination.Visible = false;
            this.btnUpdateExamination.Visible = true;
            this.btnDeleteExamination.Visible = true;

            if (this.lvExamination.SelectedItems.Count > 0)
            {
                ListViewItem ls = this.lvExamination.SelectedItems[0];

                int id = Int32.Parse(ls.SubItems[0].Text);
                PatientExamination pe = PatientExaminationDao.getById(id);
                this.cbExamType.Text = pe.ExaminationType;
                this.cbExamItem.Text = pe.ExaminationItem;
                this.tbExamResult.Text = pe.Result;
            }
        }

        private void lvDrug_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnAddDrug.Visible = false;
            this.btnUpdateDrug.Visible = true;
            this.btnDeleteDrug.Visible = true;
            if (this.lvDrug.SelectedItems.Count > 0)
            {
                ListViewItem ls = this.lvDrug.SelectedItems[0];

                int id = Int32.Parse(ls.SubItems[0].Text);
                DiagnoseDrug dd = DiagnoseDrugDao.getById(id);
                this.cbDurg.Text = dd.Name;
                this.tbDrugAmount.Text = dd.Amount.ToString();
            }
        }

        private void cbUseBasic_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbUseBasic.Checked)
            {
                DiagnoseDrugDao.insertBasicDrugs(Int32.Parse(this.tbMasterId.Text));
                MessageBox.Show("基本方药物添加成功", "提示");
            }
            this.lvDrug.Items.Clear();
            List<DiagnoseDrug> ddls = DiagnoseDrugDao.getAllDiagnose(Int32.Parse(this.tbMasterId.Text));
            if (ddls != null)
            {
                foreach (DiagnoseDrug dd in ddls)
                {
                    AddDiagnoseDrug(dd);
                }
            }
        }

        private void btnUpdateYGSTT_Click(object sender, EventArgs e)
        {
            DiagnoseYGSTT dy = DiagnoseYGSTTDao.getByDiagnoseId(Int32.Parse(this.tbMasterId.Text.ToString()));
            if (dy == null)
            {
                dy = new DiagnoseYGSTT();
            }

            dy.DiagnoseId = Int32.Parse(this.tbMasterId.Text.ToString());
            dy.MotorOrder = this.tbYMOrder.Text.Trim();
            dy.MotorFrequency = this.tbYPFrequency.Text.Trim();
            dy.MotorStrength = this.tbYMStrength.Text.Trim();
            dy.MotorComplexity = this.tbYPComplexity.Text.Trim();
            dy.MotorInterference = this.tbYPInterference.Text.Trim();
            dy.MotorScore = this.tbYPScore.Text.Trim();
            dy.PhonicOrder = this.tbYPOrder.Text.Trim();
            dy.PhonicFrequency = this.tbYPFrequency.Text.Trim();
            dy.PhonicStrength = this.tbYPStrength.Text.Trim();
            dy.PhonicComplexity = this.tbYPComplexity.Text.Trim();
            dy.PhonicInterference = this.tbYPInterference.Text.Trim();
            dy.PhonicScore = this.tbYMScore.Text.Trim();
            dy.DamageRate = this.tbYDamageRate.Text.Trim();
            dy.Severity = this.tbYSeverity.Text.Trim();

            if (dy.Id == 0)
            {
                DiagnoseYGSTTDao.insert(dy);
            }
            else
            {
                DiagnoseYGSTTDao.update(dy);
            }

        }

        private void cbShowTCM_CheckedChanged(object sender, EventArgs e)
        {

            this.gbTCM.Visible = false;
            this.gbLingualVein.Visible = false;
            this.gbConclusion.Visible = false;
            if (this.cbShowTCM.Checked)
            {
                this.dtMasterDate.Visible = false;
                this.cbDiagnoseDate.Visible = false;
                this.cbTCMDate.Visible = true;


                this.btnAddMaster.Visible = false;
                this.btnUpdateMaster.Visible = false;

                DataTable dtTCMDate = ComboxDataSourceUtil.getFollowTCMs(int.Parse(this.tbId.Text));
                this.cbTCMDate.DataSource = dtTCMDate;
                this.cbTCMDate.DisplayMember = "create_date";
                this.cbTCMDate.ValueMember = "id";


                if (this.Patient.SelectedIndex == 4 && this.cbTCMDate.SelectedValue !=null)
                {

                    TCM t = TCMDao.getById(Int32.Parse(this.cbTCMDate.SelectedValue.ToString()));
                    RefreshTCM(t);
                    this.btnAddMaster.Visible = false;
                    this.btnUpdateMaster.Visible = false;
                }


            }
            else
            {
                this.dtMasterDate.Visible = true;
                this.cbDiagnoseDate.Visible = false;
                this.cbTCMDate.Visible = false;

                this.btnAddMaster.Visible = true;
                this.btnUpdateMaster.Visible = false;

                this.dtMasterDate.Text = DateTime.Now.ToShortDateString();
            }
        }

        private void btnUpdateTCM_Click(object sender, EventArgs e)
        {
            TCM t = TCMDao.getById(Int32.Parse(this.tbMasterId.Text.Trim()));
            t.Syndrome1 = this.cbSyndrome1.Text;
            t.Syndrome2 = this.cbSyndrome2.Text;
            t.Syndrome3 = this.cbSyndrome3.Text;
            t.Syndrome4 = this.cbSyndrome4.Text;
            t.Syndrome5 = this.cbSyndrome5.Text;
            t.Syndrome6 = this.cbSyndrome6.Text;
            t.Syndrome7 = this.cbSyndrome7.Text;
            t.Syndrome8 = this.cbSyndrome8.Text;
            t.Syndrome9 = this.cbSyndrome9.Text;
            t.Syndrome10 = this.cbSyndrome10.Text;
            t.Syndrome11 = this.cbSyndrome11.Text;
            t.Syndrome12 = this.cbSyndrome12.Text;
            t.Syndrome13 = this.cbSyndrome13.Text;
            t.Syndrome14 = this.cbSyndrome14.Text;
            t.Syndrome15 = this.cbSyndrome15.Text;
            t.Syndrome16 = this.cbSyndrome16.Text;
            t.Syndrome17 = this.cbSyndrome17.Text;
            t.Syndrome18 = this.cbSyndrome18.Text;
            t.Syndrome19 = this.cbSyndrome19.Text;
            t.Syndrome20 = this.cbSyndrome20.Text;
            t.Syndrome21 = this.cbSyndrome21.Text;
            t.Syndrome22 = this.cbSyndrome22.Text;
            t.Syndrome23 = this.cbSyndrome23.Text;
            t.Syndrome24 = this.cbSyndrome24.Text;
            t.Syndrome25 = this.cbSyndrome25.Text;
            t.Syndrome26 = this.cbSyndrome26.Text;
            t.Syndrome27 = this.cbSyndrome27.Text;
            t.Syndrome28 = this.cbSyndrome28.Text;
            t.Syndrome29 = this.cbSyndrome29.Text;
            t.Syndrome30 = this.cbSyndrome30.Text;
            t.Tongue1 = this.cbTongue11.Checked.ToString() + "-" + this.cbTongue12.Checked.ToString() + "-" + this.cbTongue13.Checked.ToString() + "-" + this.cbTongue14.Checked.ToString() + "-" + this.cbTongue15.Checked.ToString();
            t.Tongue2 = this.cbTongue21.Checked.ToString() + "-" + this.cbTongue22.Checked.ToString() + "-" + this.cbTongue23.Checked.ToString() + "-" + this.cbTongue24.Checked.ToString() + "-" + this.cbTongue25.Checked.ToString() + "-" + this.cbTongue26.Checked.ToString();
            t.Tongue3 = this.cbTongue31.Checked.ToString() + "-" + this.cbTongue32.Checked.ToString() + "-" + this.cbTongue33.Checked.ToString() + "-" + this.cbTongue34.Checked.ToString() + "-" + this.cbTongue35.Checked.ToString() + "-" + this.cbTongue36.Checked + "-" + this.cbTongue37.Checked + "-" + this.cbTongue38.Checked.ToString() + "-" + this.cbTongue39.Checked.ToString() + "-" + this.cbTongue310.Checked.ToString();
            t.Pulse = this.cbPulse1.Checked.ToString() + "-" + this.cbPulse2.Checked.ToString() + "-" + this.cbPulse3.Checked.ToString() + "-" + this.cbPulse4.Checked.ToString() + "-" + this.cbPulse5.Checked.ToString() + "-" + this.cbPulse6.Checked + "-" + this.cbPulse7.Checked.ToString() + "-" + this.cbPulse8.Checked.ToString() + "-" + this.cbPulse9.Checked.ToString() + "-" + this.cbPulse10.Checked.ToString() + "-" + this.cbPulse11.Checked.ToString() + "-" + this.cbPulse12.Checked.ToString() + "-" + this.cbPulse13.Checked.ToString() + "-" + this.cbPulse14.Checked.ToString() + "-" + this.cbPulse15.Checked.ToString();
            t.Conclusion = this.tbConclusion.Text.Trim();
            t.Differentiation = this.tbDifferentiation.Text.Trim();

            TCMDao.update(t);

            MessageBox.Show("中医证候更新成功", "提示");
        }

        private void cbTCMDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            int c;
            if (this.cbTCMDate.SelectedValue != null && Int32.TryParse(this.cbTCMDate.SelectedValue.ToString(), out c))
            {

                TCM t = TCMDao.getById(Int32.Parse(this.cbTCMDate.SelectedValue.ToString()));
                RefreshTCM(t);
                this.btnAddMaster.Visible = false;
                this.btnUpdateMaster.Visible = false;
            }

        }

        private void cbDiagnoseDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            int c;
            if (this.cbDiagnoseDate.SelectedValue != null &&Int32.TryParse(this.cbDiagnoseDate.SelectedValue.ToString(), out c))
            {
                Diagnose d = DiagnoseDao.getById(Int32.Parse(this.cbDiagnoseDate.SelectedValue.ToString()));
                RefreshDiagnose(d, false);
                this.btnAddMaster.Visible = false;
                this.btnUpdateMaster.Visible = false;
            }
        }

        private void cbUseLastDrugs_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbUseLastDrugs.Checked)
            {
                //todo 使用上次药方方法
                DiagnoseDao.insertLastDrugs(Int32.Parse(this.tbId.Text.Trim()), Int32.Parse(this.tbMasterId.Text));
                MessageBox.Show("上次药方药物添加成功", "提示");
            }
            this.lvDrug.Items.Clear();
            List<DiagnoseDrug> ddls = DiagnoseDrugDao.getAllDiagnose(Int32.Parse(this.tbMasterId.Text));
            if (ddls != null)
            {
                foreach (DiagnoseDrug dd in ddls)
                {
                    AddDiagnoseDrug(dd);
                }
            }
        }

    }
}
